package com.lanchetech.service.impl;


import com.alibaba.fastjson.JSON;
import com.lanchetech.bean.dto.VipConfigDTO;
import com.lanchetech.common.enums.ConsumptionRecordTypeEnum;
import com.lanchetech.common.enums.UserStatusEnum;
import com.lanchetech.common.enums.WebConfigEnum;
import com.lanchetech.dao.ConsumptionAmountMapper;
import com.lanchetech.dao.ConsumptionRecordMapper;
import com.lanchetech.dao.UserMapper;
import com.lanchetech.dao.WebConfigMapper;
import com.lanchetech.entity.ConsumptionAmount;
import com.lanchetech.entity.ConsumptionRecord;
import com.lanchetech.entity.User;
import com.lanchetech.service.ConsumptionService;
import com.lanchetech.service.GroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class ConsumptionServiceImpl implements ConsumptionService {

    @Autowired
    ConsumptionAmountMapper consumptionAmountMapper;

    @Autowired
    ConsumptionRecordMapper consumptionRecordMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    WebConfigMapper webConfigMapper;

    @Autowired
    GroupService groupService;

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public void updateConsumptionAmount(ConsumptionRecord record) {
        ConsumptionAmount amount = consumptionAmountMapper.findOneByUserId(record.getUserId());

        record.setType(ConsumptionRecordTypeEnum.getTypeByRecordType(record.getRecordType()));

        BigDecimal amountDiff = record.getType().equals((byte) 0) ? record.getAmount() : record.getAmount().negate();

        Long orderCountDiff = ConsumptionRecordTypeEnum.CONSUMPTION.getRecordType().equals(record.getRecordType()) ? 1L : 0L;

        BigDecimal finalAmount = amountDiff;

        if(amount == null){
            consumptionAmountMapper.insert(ConsumptionAmount.builder()
                    .userId(record.getUserId())
                    .amount(finalAmount)
                    .historyAmount(finalAmount)
                    .orderCount(orderCountDiff)
                    .createdAt(new Date())
                    .updatedAt(new Date())
                    .build());
        }else{
            finalAmount = amount.getAmount().add(amountDiff);
            amount.setAmount(finalAmount);
            amount.setHistoryAmount(amount.getHistoryAmount().add(amountDiff));
            amount.setOrderCount(amount.getOrderCount() + orderCountDiff);
            amount.setUpdatedAt(new Date());
            consumptionAmountMapper.updateByPrimaryKey(amount);
        }

//        String config = webConfigMapper.findValueByName(WebConfigEnum.VIP_CONFIG.getConfig());
//        VipConfigDTO dto = JSON.parseObject(config, VipConfigDTO.class);
//
//        Integer level = null;
//        if(finalAmount.compareTo(dto.getV5()) >= 0){
//            level = 5;
//        }else if(finalAmount.compareTo(dto.getV4()) >= 0){
//            level = 4;
//        }else if(finalAmount.compareTo(dto.getV3()) >= 0){
//            level = 3;
//        }else if(finalAmount.compareTo(dto.getV2()) >= 0){
//            level = 2;
//        }else if(finalAmount.compareTo(dto.getV1()) >= 0){
//            level = 1;
//        }
//
//        User user = userMapper.selectByPrimaryKey(record.getUserId());
//        // 只能升，不可以降；降由管理员来处理。被禁止分销的用户，不可以再升级
//        if(level != null && level > user.getLevel() && UserStatusEnum.NORMAL.getStatus().equals(user.getStatus())){
//            groupService.updateUserLevel(level, user.getId());
//        }

        record.setUserId(record.getUserId());
        record.setCreatedAt(new Date());
        consumptionRecordMapper.insert(record);
    }
}
